package net.appositedesigns.appshare;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class AppList extends ListActivity {
	
	
	private ListView appList;
	private AppListAdapter adapter;
	
	private List<AppListEntry> apps = new ArrayList<AppListEntry>();
	private AppFinder appFinder;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        appFinder = new AppFinder(this);
        appList = getListView();
        adapter = new AppListAdapter(this, apps);
        appList.setAdapter(adapter);
        
        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          fetchApps(query);
        }
        else
        {
        	fetchApps();
        }
        appList.setOnItemClickListener(new OnItemClickListener() {
        	
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                  AppListEntry app = (AppListEntry)appList.getAdapter().getItem(position);
                  share(app);
                }
         });
    }
    
    private void fetchApps(String query) {

    	appFinder.execute(query);
	}

	@Override
    public boolean onSearchRequested() {
    	return super.onSearchRequested();
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

    	 switch (getListView().getChoiceMode()) {
		case ListView.CHOICE_MODE_MULTIPLE:
			menu.findItem(R.id.menu_multiselect).setVisible(true);
    	 
		case ListView.CHOICE_MODE_NONE:
    	 {
    		 menu.findItem(R.id.menu_multiselect).setVisible(false);
    	 }

		default:
			break;
		}
    	 menu.findItem(R.id.menu_multiselect).setVisible(false);
    	return super.onPrepareOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch (item.getItemId()) {
		case  R.id.menu_multiselect:
			getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
			return true;

		default:
			super.onOptionsItemSelected(item);
			break;
		}
        
        return true;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    private void fetchApps() {
		appFinder.execute();
	}

	public void share(AppListEntry app) {
    	final Intent intent = new Intent(Intent.ACTION_SEND);

    	intent.setType("text/plain");
    	String text = getString(R.string.default_text, app.getAppName(), "https://market.android.com/details?id="+app.getPackageName());
    	intent.putExtra(Intent.EXTRA_TEXT, text);
    	intent.putExtra(Intent.EXTRA_SUBJECT, app.getAppName());

    	startActivity(Intent.createChooser(intent,
    	"Share via"));
    	}

	public void setApps(List<AppListEntry> result) {
		
		apps.clear();
		apps.addAll(result);
		
		adapter.notifyDataSetChanged();
		
		
	}
	
}